package org.jeecg.modules.workflow.convert.task;

import org.flowable.common.engine.impl.db.SuspensionState;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.collection.CollectionUtils;
import org.jeecg.common.workflow.vo.task.task.BpmTaskDonePageItemRespVO;
import org.jeecg.common.workflow.vo.task.task.BpmTaskRespVO;
import org.jeecg.common.workflow.vo.task.task.BpmTaskTodoPageItemRespVO;
import org.jeecg.modules.workflow.entity.BpmTaskExt;
import org.jeecg.modules.workflow.service.message.dto.BpmMessageSendWhenTaskCreatedReqDTO;

import java.util.List;
import java.util.Map;

/**
 * Bpm 任务 Convert
 *
 * @author 芋道源码
 */
public interface BpmTaskConvert {


    default List<BpmTaskTodoPageItemRespVO> convertList1(List<Task> tasks, Map<String, ProcessInstance> processInstanceMap,
                                                         Map<String, LoginUser> userMap) {
        return CollectionUtils.convertList(tasks, task -> {
            BpmTaskTodoPageItemRespVO respVO = convert1(task);
            ProcessInstance processInstance = processInstanceMap.get(task.getProcessInstanceId());
            if (processInstance != null) {
                LoginUser startUser = userMap.get(processInstance.getStartUserId());
                respVO.setProcessInstance(convert(processInstance, startUser));
            }
            return respVO;
        });
    }

    BpmTaskTodoPageItemRespVO convert1(Task bean);

    default Integer convertSuspendedToSuspensionState(boolean suspended) {
        return suspended ? SuspensionState.SUSPENDED.getStateCode() :
                SuspensionState.ACTIVE.getStateCode();
    }

    default List<BpmTaskDonePageItemRespVO> convertList2(List<HistoricTaskInstance> tasks, Map<String, BpmTaskExt> bpmTaskExtDOMap,
                                                         Map<String, HistoricProcessInstance> historicProcessInstanceMap,
                                                         Map<String, LoginUser> userMap) {
        return CollectionUtils.convertList(tasks, task -> {
            BpmTaskDonePageItemRespVO respVO = convert2(task);
            BpmTaskExt taskExtDO = bpmTaskExtDOMap.get(task.getId());
            copyTo(taskExtDO, respVO);
            HistoricProcessInstance processInstance = historicProcessInstanceMap.get(task.getProcessInstanceId());
            if (processInstance != null) {
                LoginUser startUser = userMap.get(processInstance.getStartUserId());
                respVO.setProcessInstance(convert(processInstance, startUser));
            }
            return respVO;
        });
    }

    BpmTaskDonePageItemRespVO convert2(HistoricTaskInstance bean);

    BpmTaskExt convert(org.flowable.task.api.Task bean);

    default List<BpmTaskRespVO> convertList3(List<HistoricTaskInstance> tasks, Map<String, BpmTaskExt> bpmTaskExtDOMap,
                                             HistoricProcessInstance processInstance, Map<String, LoginUser> userMap) {
        return CollectionUtils.convertList(tasks, task -> {
            BpmTaskRespVO respVO = convert3(task);
            BpmTaskExt taskExtDO = bpmTaskExtDOMap.get(task.getId());
            copyTo(taskExtDO, respVO);
            if (processInstance != null) {
                LoginUser startUser = userMap.get(processInstance.getStartUserId());
                respVO.setProcessInstance(convert(processInstance, startUser));
            }
            LoginUser assignUser = userMap.get(task.getAssignee());
            if (assignUser != null) {
                respVO.setAssigneeUser(convert3(assignUser));
                /*if (assignUser.getDept() != null) {
                    respVO.getAssigneeUser().setDeptName(assignUser.getDept().getDeptName());
                }*/
            }
            return respVO;
        });
    }

    BpmTaskRespVO convert3(HistoricTaskInstance bean);
    BpmTaskRespVO.User convert3(LoginUser bean);

    void copyTo(BpmTaskExt from, BpmTaskDonePageItemRespVO to);

    BpmTaskTodoPageItemRespVO.ProcessInstance convert(ProcessInstance processInstance, LoginUser startUser);

    BpmTaskTodoPageItemRespVO.ProcessInstance convert(HistoricProcessInstance processInstance, LoginUser startUser);

    default BpmMessageSendWhenTaskCreatedReqDTO convert(ProcessInstance processInstance, LoginUser startUser, org.flowable.task.api.Task task) {
        BpmMessageSendWhenTaskCreatedReqDTO reqDTO = new BpmMessageSendWhenTaskCreatedReqDTO();
        copyTo(processInstance, reqDTO);
        copyTo(startUser, reqDTO);
        copyTo(task, reqDTO);
        return reqDTO;
    }
    void copyTo(ProcessInstance from, BpmMessageSendWhenTaskCreatedReqDTO to);
    void copyTo(LoginUser from, BpmMessageSendWhenTaskCreatedReqDTO to);
    void copyTo(org.flowable.task.api.Task task, BpmMessageSendWhenTaskCreatedReqDTO to);
}
